package _01_quick_start._8_2_1_reusable_pieces

import java.nio.file.Paths

import akka.actor.ActorSystem
import akka.stream._
import akka.stream.scaladsl._
import akka.util.ByteString
import akka.{Done, NotUsed}

import scala.concurrent._

object Main extends App {

  implicit val system       = ActorSystem("QuickStart")
  implicit val materializer = ActorMaterializer()
  implicit val ec           = system.dispatcher

  // source
  val source: Source[Int, NotUsed] = Source(1 to 100)

  // flow
  val flow = Flow[Int]
             .scan(BigInt(1))((acc,next) => acc * next)
             .map(_.toString)
             .map(s => ByteString(s + "\n"))
  // sink
  val sink: Sink[ByteString,Future[IOResult]] = FileIO.toPath(Paths.get("factorial2.txt"))

  val done:Future[IOResult] = source.via(flow).toMat(sink)(Keep.right).run()

  done.onComplete(_ => system.terminate())
}
